﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class addEditLateAbsences : Form
    {
        private int type;
        editEmployeeRecordsModule parent;

        public addEditLateAbsences(editEmployeeRecordsModule parent,int type)
        {
            this.type = type;
            this.parent = parent;
            InitializeComponent();
            initializeLabel();
            
            if (type == 3)
            {
                attendanceTextBox.Value = parent.getAbsences();
            }
            else if (type == 2)
            {
                attendanceTextBox.Value = parent.getLates();
            }
        }

        private void cancelLateAbsences_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void okLateAbsences_Click(object sender, EventArgs e)
        {
            attendance value = new attendance();
            value.setValue((int)attendanceTextBox.Value);
            if (type == 1)
            {
                bool success = new LateAbsenceController().addAbsent(parent.getSelectedLastName(), parent.getSelectedFirstName(), value);
                if (success)
                {
                    parent.initializeContent();
                    parent.getEmployeesList().SelectedIndex = parent.getEmployeesList().Items.IndexOf(parent.getSelectedLastName() +", "+ parent.getSelectedFirstName());
                    this.Close();
                }
                else
                    this.Close();
            }
            else if (type == 3)
            {
                bool success = new LateAbsenceController().editAbsences(value, parent.getSelectedLastName(), parent.getSelectedFirstName(), parent.getAbsentSelectedDate());
                if (success)
                {
                    parent.initializeContent();
                    parent.getEmployeesList().SelectedIndex = parent.getEmployeesList().Items.IndexOf(parent.getSelectedLastName() + ", " + parent.getSelectedFirstName());
                    this.Close();
                }
            }
            else if (type == 0)
            {
                bool success = new LateAbsenceController().addLates(parent.getSelectedLastName(), parent.getSelectedFirstName(), value);
                if (success)
                {
                    parent.initializeContent();
                    parent.getEmployeesList().SelectedIndex = parent.getEmployeesList().Items.IndexOf(parent.getSelectedLastName() + ", " + parent.getSelectedFirstName());
                    this.Close();
                }
                else
                    this.Close();
            }
            else if (type == 2)
            {
                bool success = new LateAbsenceController().editLates(value, parent.getSelectedLastName(), parent.getSelectedFirstName(), parent.getAbsentSelectedDate());
                if (success)
                {
                    parent.initializeContent();
                    parent.getEmployeesList().SelectedIndex = parent.getEmployeesList().Items.IndexOf(parent.getSelectedLastName() + ", " + parent.getSelectedFirstName());
                    this.Close();
                }
            }
        }

        private void initializeLabel()
        {
            if (type == 0 || type == 2)
            {
                attendanceLabel.Text = "Minutes late:";
            }
            else
            {
                attendanceLabel.Text = "Times absent:";
            }
        }

        public NumericUpDown getTextBox()
        {
            return attendanceTextBox;
        }
    }
}
